'''
 * @Author: Benjay·Shaw
 * @Date: 2024-10-31 17:07:51
 * @LastEditors: Benjay·Shaw
 * @LastEditTime: 2024-10-31 22:45:24
 * @Description: 验证模块
'''
import paddle
import cv2
import numpy as np
from tqdm import tqdm
from utils.my_net import MyNet


def val(opt, loader_val, model_path):
    model = MyNet(opt, eval_mode=True)
    model.net.load_state_dict(model.load(model_path)['net'])
    with paddle.no_grad():
        loop = tqdm(enumerate(loader_val), total=len(loader_val))
        val_epoch_loss = 0
        for i, (data_prev, data_now, label, _) in loop:
            model.set_input(data_prev, data_now, label)
            model.forward()
            label = model.label.to('float32')
            result = model.net.forward(opt.img_pre, opt.img_now)
            label = paddle.squeeze(x=model.label)
            label = label.astype(dtype='int64')
            val_loss = model.loss(model.img_pre, model.img_now, result, label)
            val_epoch_loss += val_loss.item()
            loop.set_postfix(loss=val_loss.item())
        val_epoch_loss /= len(loader_val)
        return val_epoch_loss
